import { _decorator, Component, Node } from 'cc';
import { GlobalEnum } from '../../../Init/Config/GlobalEnum';
import { EventTypes } from '../../../Init/Managers/EventTypes';
import GlobalPool from '../../../Init/Tools/GlobalPool';
import { BasicLayer } from '../Common/Basic/BasicLayer';
import { MappingBullet } from './MappingBullet';
const { ccclass, property } = _decorator;
/**3D */
@ccclass('BulletLayer')
export class BulletLayer extends BasicLayer {
    protected _bullets: { [uuid: string]: MappingBullet } = {};
    // #region -------------------------------层级生命周期------------
    /**初始化 只执行一次*/
    protected init() {

    };
    /**注册通过自定义事件管理器管理的事件  */
    protected onEvents() {
        this.on(EventTypes.Mapping3DEvents.SetBullets, this.onSetBullets, this);
    };
    /**设置状态、数据等, */
    public setData(data?: any) {

    };
    /**重置状态、数据等，子类实现 ,注销事件*/
    public reset() {
        this._bullets = {};
    }

    public customUpdate(dt: number) {
        for (const key in this._bullets) {
            const b = this._bullets[key];
            if (b && b.node.active) {
                b.update(dt);
            } else {
                delete this._bullets[key];
            }
        }
    }
    public customLateUpdate(dt: number) {

    }
    // #endregion

    //#region ----------------------创建子弹3d
    //创建子弹3d
    private createBullet(d: { t: GlobalEnum.BulletType, node: Node, h: number, state: number }) {
        let _name = d.t == GlobalEnum.BulletType.normalBullet ? 'normalBullet3d' : 'throughBullet3d';
        let e = GlobalPool.get(_name, d);
        e.parent = this.node;
        let cmp = new MappingBullet(e, d);
        cmp.init(d.h);
        this._bullets[e.uuid] = cmp;
    }
    //#endregion

    //#region ----------------------事件-----------
    public onSetBullets(d: { t: GlobalEnum.BulletType, node: Node, h: number, state: number }) {
        this.createBullet(d);

    }

    //#endregion
}

